package com.ljx.springboot003.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.springboot003.entity.Classes;
import com.ljx.springboot003.entity.Student;
import com.ljx.springboot003.service.ClassesService;
import com.ljx.springboot003.service.StudentService;
import com.ljx.springboot003.mapper.StudentMapper;
import com.ljx.springboot003.vo.StudentVO;
import com.ljx.springbootcommon.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author linjingxiong
* @description 针对表【student】的数据库操作Service实现
* @createDate 2025-01-07 14:11:03
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{
    @Autowired
    private ClassesService classesService;
    // 1. 分页查询
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
        // 1. 开始分页
        Page<Student> page1 = this.page(new Page<>(page, pageSize));
        
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    
    // 3. 条件带分页查询
    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVO vo) {
        // 1. 进行分页带条件查询
        // 1.1 封装查询条件
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getName()), "name", vo.getName());
        wrapper.eq(vo.getAge()!=null&&vo.getAge()!=0, "age", vo.getAge());
        wrapper.eq(vo.getClassId() != null && vo.getClassId() != 0, "class_id", vo.getClassId());
        // 1.2 开始分页查询
        Page<Student> page1 = this.page(new Page<>(page, pageSize), wrapper);
        // 1.3 处理学生所在班级名称
        page1.getRecords().forEach(st -> {
            Classes classes = classesService.getById(st.getClassId());
            st.setClassName(classes.getClassName());
        });
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    // 添加学生
    @Override
    public boolean add(Student student) {
        return this.save(student) ;
    }
}




